package activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;
import android.widget.Toolbar;

import com.example.jaden.washeradministrator.*;
import com.example.jaden.washeradministrator.MyApplication;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.MalformedURLException;
import java.util.List;

import customControls.DormitoryAdapter;
import dormitory.Dormitory;
import washer.Washer;

public class HomePage extends AppCompatActivity implements AdapterView.OnItemClickListener, CompoundButton.OnCheckedChangeListener {

    MyApplication application;
    ListView dorm;
    List<Dormitory> dormitories;
    DormitoryAdapter dormitoryAdapter;
    boolean isopen=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homepage);
        //将当前活动加入list中进行管理
        ActivityManagement.activityList.add(this);

        //获取Toolbar的实例
        android.support.v7.widget.Toolbar toolbar=(android.support.v7.widget.Toolbar)findViewById(R.id.homepage_toolbar);
        setSupportActionBar(toolbar);
        getInstance();
        initDorm();
        dorm.setOnItemClickListener(this);
       /*
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialogShow();
            }
        });
    */
    }
    //加载toolbar菜单文件
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar,menu);
        return true;
    }
    //处理菜单中按钮的点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.add:
                dialogShow();
                break;
        }
        return true;
    }
    //点击新增宿舍的+号按钮
    private void dialogShow()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(R.layout.adddorm_dialog, null);
        final EditText dormloc=(EditText)v.findViewById(R.id.editText_dormloc_adddormdialog);
        final EditText washernumber=(EditText)v.findViewById(R.id.editText_washernumber_adddormdialog);
        final EditText freewashernumber=(EditText)v.findViewById(R.id.editText_freewashernumber_adddormdialog);
        RadioButton open=(RadioButton)v.findViewById(R.id.radioButton_open_adddormdialog);
        RadioButton close=(RadioButton)v.findViewById(R.id.radioButton_close_adddormdialog);
        Button back=(Button)v.findViewById(R.id.button_back_adddormdialog);
        Button confirm=(Button)v.findViewById(R.id.button_confirm_adddormdialog);

        final Dialog dialog=builder.create();
        dialog.show();
        dialog.getWindow().setContentView(v);
        dialog.getWindow().setGravity(Gravity.BOTTOM);

        open.setOnCheckedChangeListener(this);
        close.setOnCheckedChangeListener(this);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String dormLocation=dormloc.getText().toString();
                int washerNumber=Integer.parseInt(washernumber.getText().toString());
                int freeNumber=Integer.parseInt(freewashernumber.getText().toString());

                    /**
                     * 新增宿舍
                     */
                    try {
                        String url="http://47.96.159.236:8080/Wash/Dormitory?method=insertDormitory";
                        JSONObject userJson=new JSONObject();
                        userJson.put("DormLocation",dormLocation);
                        userJson.put("IsOpen",isopen);
                        userJson.put("WasherNumber",washerNumber);
                        userJson.put("FreeNumber",freeNumber);
                        String messageJson=String.valueOf(userJson);
                        TransferData.AddDorm(url,messageJson);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    while(true) {
                        if(application.isAddDormOK()) {
                            application.setAddDormOK(false);
                            //Order order= (Order) application.getResponseMessage().getData();
                            //application.
                            initDorm();
                            dialog.dismiss();
                            break;
                        }
                    }
            }
        });
    }
    //获取控件实例
    private void getInstance() {
        dorm=(ListView)findViewById(R.id.ListView_dormwashmachine_homepage);
        application=MyApplication.getInstance();
        //imageButton=(ImageButton)findViewById(R.id.imageButton_adddorm_homepage);
    }
    //初始化商品
    private void initDorm() {
        String url="http://47.96.159.236:8080/Wash/Dormitory?method=getDormitory";
        try {
            Gson gson=new Gson();
            //Dormitory dormitory=new Dormitory();
            //String message="{\"DormID\":\"\",\"DormLocation\":\"\", \"IsOpen\":, \"WasherNumber\":, \"FreeNumber\": }";
            String message="{ \"DormID\":\"\"}";
            TransferData.GetDormList(url,message);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        while(true) {
            if(application.isGetDormOK()) {
                application.setGetDormOK(false);
                dormitories= (List<Dormitory>) application.getResponseMessage().getData();
                dormitoryAdapter = new DormitoryAdapter(HomePage.this, R.layout.dorm_listview_item_washingmachine, dormitories);
                dorm.setAdapter(dormitoryAdapter);
                break;
            }
        }
    }

    /**
     *点击item进入某一宿舍 查看该宿舍的具体洗衣机使用情况
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Dormitory dormitory = dormitories.get(position);
        //如果该宿舍楼显示是营业中则获取该宿舍楼楼的洗衣机
        if (dormitory.getIsOpen()) {
            application.setCurDorm(dormitory);
            Intent intent = new Intent(this, WasherPage.class);
            startActivity(intent);
        }
        else
            Toast.makeText(HomePage.this,dormitory.getDormLocation()+"处于未营业状态",Toast.LENGTH_LONG).show();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
         case R.id.radioButton_open_adddormdialog:
             isopen=true;
             break;
            case R.id.radioButton_close_adddormdialog:
                isopen=false;
                break;
        }

    }
}
